package com.cs.beneficios.web.action;

import com.cs.beneficios.business.entity.RepBeneficioOtorgadoDAO;
import com.cs.beneficios.business.entity.RepDetBeneficiarioDAO;
import com.cs.beneficios.business.entity.RepDetCruceDAO;
import com.cs.beneficios.business.model.RepDetCruceVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;

import java.io.IOException;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Collection;

//import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ReportDetCruceAction extends Action {
    /**This is the main action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
                                 HttpServletRequest request, 
                                 HttpServletResponse response) throws IOException, 
                                                                      ServletException {
    String codePrograma =  (String)request.getParameter("codePrograma");
    String codeDestino = (String)request.getParameter("codeDestino");
    String codeSubprograma = (String)request.getParameter("codeSubPrograma");
    String codeSubDestino = (String)request.getParameter("codeSubDestino");
    String fechaInicial = (String)request.getParameter("fechaInicial");
    String fechaFinal = (String)request.getParameter("fechaFinal");
    String inclusion = (String)request.getParameter("inclusion");
    String accion = (String)request.getParameter("acciones");
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    HttpSession session = request.getSession();
    UserVO user = (UserVO)session.getAttribute("userInfo");
    session.removeAttribute("msg");
    session.setAttribute("msg","");
    Collection programasF=null;
    Collection subprogramasF=null;
    Collection programasD = null;
    Collection subprogramasD = null;
    RepDetBeneficiarioDAO objDetBene;
    session.removeAttribute("Beneficiario");
    session.removeAttribute("errorfecha");
    RepDetCruceDAO objRepDetCruce;
    
      try
      {
        RepBeneficioOtorgadoDAO objBeneOtor1DAO = new RepBeneficioOtorgadoDAO(cf);
        programasF = objBeneOtor1DAO.findProgramaByUserReport(user.getCode(),user.getUserGroupName(), user.getCodeArea());
        cf.closeConnection(objBeneOtor1DAO.getConn());
      }catch(SQLException e){
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSSPE - cwsub ********/");
      }
      session.removeAttribute("msg");
      if(programasF.size() > 0){
        session.setAttribute("programasF",programasF);
      }else
      {
        session.setAttribute("msg","No se encontraron programas vigentes a la fecha..");
      }
      
      if(codePrograma != null)
      {
        if(!codePrograma.equals("nulo"))
        {
          try
          {
            RepBeneficioOtorgadoDAO objBeneOtor2DAO = new RepBeneficioOtorgadoDAO(cf);
            subprogramasF = objBeneOtor2DAO.findSubProgramaByUserReport(user.getCode(),user.getUserGroupName(),codePrograma,user.getCodeArea());
            programasD = objBeneOtor2DAO.findProgramaByUserReport(user.getCode(),user.getUserGroupName(), user.getCodeArea());
            cf.closeConnection(objBeneOtor2DAO.getConn());
          }catch(SQLException e){
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
          }
          session.removeAttribute("msg");
          if(subprogramasF.size() > 0){
            session.setAttribute("subprogramasF",subprogramasF);
          }else
          {
            session.setAttribute("msg","No se encontraron subprogramas asociados a este programa fuente que esten vigentes..");
          }
          
          if(programasD.size() > 0){
              session.setAttribute("programasD",programasD);
          }else{
              session.setAttribute("msg","No se encontraron programas vigentes a la fecha..");
          }
        }else{
            session.setAttribute("msg","");
            session.removeAttribute("subprogramasF");
        }
      }
      
      if(codeDestino != null)
     {
      if(!codeDestino.equals("nulo"))
      {
        try
        {
          RepBeneficioOtorgadoDAO objBeneOtor3DAO = new RepBeneficioOtorgadoDAO(cf);
          subprogramasD = objBeneOtor3DAO.findSubProgramaByUserReport(user.getCode(),user.getUserGroupName(),codeDestino,user.getCodeArea());
          cf.closeConnection(objBeneOtor3DAO.getConn());
        }catch(SQLException e){
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
        session.removeAttribute("msg");
        if(subprogramasD.size() > 0){
          session.setAttribute("subprogramasD",subprogramasD);
        }else
        {
          session.setAttribute("msg","No se encontraron subprogramas asociados a este programa destino que esten vigentes..");
        }
      }else{
          session.setAttribute("msg","");
          session.removeAttribute("subprogramasD");
      }
    }
                
    session.removeAttribute("beneficiosFuente");   
    if(accion != null && accion.equals("BuscarTodo"))
    {
        ArrayList beneficiariosCruce = null;
        try
        {
          RepDetCruceVO objDetCruceVO = new RepDetCruceVO();
          
          if(codePrograma != null && !codePrograma.equals("nulo"))
          {
            objDetCruceVO.setCodeProgramaFuente(new Long((String)codePrograma));
          }
          if(codeSubprograma != null && !codeSubprograma.equals("nulo"))
          {
            objDetCruceVO.setCodeSubprogramaFuente(new Long((String)codeSubprograma));
          }
          if(codeDestino != null && !codeDestino.equals("nulo"))
          {
            objDetCruceVO.setCodeProgramaDestino(new Long((String)codeDestino));
          }
          if(codeSubDestino != null && !codeSubDestino.equals("nulo"))
          {
            objDetCruceVO.setCodeSubprogramaDestino(new Long((String)codeSubDestino));
          }
          if(fechaInicial != null && !fechaInicial.equals(""))
          {
            objDetCruceVO.setDesde(fechaInicial);
          }
          if(fechaFinal != null && !fechaFinal.equals(""))
          {
            objDetCruceVO.setHasta(fechaFinal);
          }
          objDetCruceVO.setInclusion(inclusion);
                   
          objRepDetCruce = new RepDetCruceDAO(cf);
          beneficiariosCruce = objRepDetCruce.getBeneficiariosCruce(objDetCruceVO);
          
          
          if(beneficiariosCruce != null)
          {
              if(beneficiariosCruce.size() > 0)
              {
                session.setAttribute("Beneficiario",beneficiariosCruce);
                
                if((codeDestino != null && !codeDestino.equals("nulo")) || (codeSubDestino != null && !codeSubDestino.equals("nulo")) || inclusion.equals("SoloFuente")){
                    objDetCruceVO.setCantidadBeneficiarios(new Long(beneficiariosCruce.size()));
                }else{
		    objDetCruceVO.setCantidadBeneficiarios(objRepDetCruce.getCantidadBeneficiarios(objDetCruceVO));			
		}                     
                
                objRepDetCruce.getFilters(objDetCruceVO);
                session.setAttribute("Parametros",objDetCruceVO);
              }else
              {
                session.removeAttribute("Beneficiario");
              }
            objDetCruceVO = null;
            cf.closeConnection(objRepDetCruce.getConn());
          }else
          {
            session.setAttribute("msg","No hay registros que cumplan con los filtros seleccionados");
            return mapping.findForward("success");
          }
        }catch(SQLException e)
        {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
    }
    
      Object obIR = request.getParameter("seleccionar");
      if(obIR == null || accion.equals("BuscarSubprogramas"))
      {
        return mapping.findForward("success");
      }else{
        return mapping.findForward("ir");
      }    
  } 
}

